<?php
/**
 * Created by PhpStorm.
 * User: Jiawei
 * Date: 2017/7/29
 * Time: 10:04
 */

namespace App\Lib\WeApp;


use App\Lib\WeApp\Api\BaseApi;
use App\Lib\WeApp\Api\CustomMsg;
use App\Lib\WeApp\Api\DeliverInfoManage;
use App\Lib\WeApp\Api\MessageEvent;
use App\Lib\WeApp\Api\MpServicer;
use App\Lib\WeApp\Api\QRCode;
use App\Lib\WeApp\Api\SessionKey;
use App\Lib\WeApp\Api\Statistic;
use App\Lib\WeApp\Api\TemplateMsg;
use App\Lib\WeApp\Api\WxLink;
use App\Wen\Utils\Settings;

class WeApp
{
	private $appid;
	private $secret;
	private $platform;
	private $instance;

	public function __construct($platform = 'mini'){
	    if($platform == 'mp'){
            $this->appid = Settings::get('mp_app_id', '');
            $this->secret = Settings::get('mp_app_secret', '');
            $this->platform = 'mp';

        }else{
            $this->appid = Settings::get('app_id');
            $this->secret = Settings::get('app_secret');
            $this->platform = 'mini';
        }
		$this->instance = [];
	}

	/**
	 * @param $code
	 * @return array sessionkey相关数组
	 */
	public function getSessionKey($code){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
		if(!isset($this->instance['sessionkey'])){
			$this->instance['sessionkey'] = new SessionKey($this->appid,$this->secret, $this->platform);
		}
		return $this->instance['sessionkey']->get($code);
	}



	/**
	 * @return TemplateMsg 模板消息对象
	 */
	public function getTemplateMsg(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
		if(!isset($this->instance['template'])){
			$this->instance['template'] = new TemplateMsg($this->appid,$this->secret, $this->platform);
		}
		return $this->instance['template'];
	}

	/**
	 * @return QRCode 二维码对象
	 */
	public function getQRCode(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
		if(!isset($this->instance['qrcode'])){
			$this->instance['qrcode'] = new QRCode($this->appid,$this->secret, $this->platform);
		}
		return $this->instance['qrcode'];
	}

	/**
	 * @return Statistic 数据统计对象
	 */
	public function getStatistic(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
		if(!isset($this->instance['statistic'])){
			$this->instance['statistic'] = new Statistic($this->appid,$this->secret, $this->platform);
		}
		return $this->instance['statistic'];
	}

	/**
	 * @return CustomMsg 客户消息对象
	 */
	public function getCustomMsg(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
		if(!isset($this->instance['custommsg'])){
			$this->instance['custommsg'] = new CustomMsg($this->appid,$this->secret, $this->platform);
		}
		return $this->instance['custommsg'];
	}

    /**
     * @return WxLink 微信链接对象
     */
    public function getWxLink(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
        if(!isset($this->instance['wxlink'])){
            $this->instance['wxlink'] = new WxLink($this->appid,$this->secret, $this->platform);
        }
        return $this->instance['wxlink'];
    }

    /**
     * @return WxLink 微信小程序发货信息管理对象
     */
    public function getDeliverInfoManage(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
        if(!isset($this->instance['deliverinfomanage'])){
            $this->instance['deliverinfomanage'] = new DeliverInfoManage($this->appid,$this->secret, $this->platform);
        }
        return $this->instance['deliverinfomanage'];
    }


    /**
     * @return MpServicer 小程序推送
     */
    public function getMessageEvent(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
        if(!isset($this->instance['messageevent'])){
            $this->instance['messageevent'] = new MessageEvent($this->appid,$this->secret, Settings::get('app_message_token', ''), Settings::get('app_message_aes_key', ''));
        }
        return $this->instance['messageevent'];
    }


    /**
     * @return MpServicer 公众号对象
     */
    public function getMpServicer(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
        if(!isset($this->instance['mpservicer'])){
            $this->instance['mpservicer'] = new MpServicer($this->appid,$this->secret, Settings::get('mp_app_token', ''), Settings::get('mp_app_aes_key', ''));
        }
        return $this->instance['mpservicer'];
    }


	public function getAccessToken(){
        if(_empty_($this->appid) || _empty_($this->secret)){
            return null;
        }
        return (new BaseApi($this->appid, $this->secret, $this->platform))->getAccessToken();
    }

}
